home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_005 / cons / cons.c < prev   
C/C++ Source or Header  |  1992-05-06  |  16KB  |  544 lines

  1. /*
  2.  *
  3.  *    DISCLAIMER:
  4.  *
  5.  *    This program is provided as a service to the programmer
  6.  *    community to demonstrate one or more features of the Amiga
  7.  *    personal computer.  These code samples may be freely used
  8.  *    for commercial or noncommercial purposes.
  9.  * 
  10.  *     Commodore Electronics, Ltd ("Commodore") makes no
  11.  *    warranties, either expressed or implied, with respect
  12.  *    to the program described herein, its quality, performance,
  13.  *    merchantability, or fitness for any particular purpose.
  14.  *    This program is provided "as is" and the entire risk
  15.  *    as to its quality and performance is with the user.
  16.  *    Should the program prove defective following its
  17.  *    purchase, the user (and not the creator of the program,
  18.  *    Commodore, their distributors or their retailers)
  19.  *    assumes the entire cost of all necessary damages.  In 
  20.  *    no event will Commodore be liable for direct, indirect,
  21.  *    incidental or consequential damages resulting from any
  22.  *    defect in the program even if it has been advised of the 
  23.  *    possibility of such damages.  Some laws do not allow
  24.  *    the exclusion or limitation of implied warranties or
  25.  *    liabilities for incidental or consequential damages,
  26.  *    so the above limitation or exclusion may not apply.
  27.  *
  28.  */
  29.  
  30. /* cons.c - a console device demo program with supporting macro routines. */
  31.  
  32. /* This program is supported by the Amiga C compiler, version 1.1 and beyond.
  33.  * (v1.0 compiler has difficulties if string variables do not have their
  34.  * initial character aligned on a longword boundary.  Compiles acceptably
  35.  * but won't run correctly.)
  36.  *
  37.  * Author:  Rob Peck, 12/1/85
  38.  * 
  39.  * This code may be freely utilized to develop programs for the Amiga.
  40.  *
  41.  */
  42.  
  43. #include "exec/types.h"
  44. #include "exec/io.h"
  45. #include "exec/memory.h"
  46.  
  47. #include "graphics/gfx.h"
  48. #include "hardware/dmabits.h"
  49. #include "hardware/custom.h"
  50. #include "hardware/blit.h"
  51. #include "graphics/gfxmacros.h"
  52. #include "graphics/copper.h"
  53. #include "graphics/view.h"
  54. #include "graphics/gels.h"
  55. #include "graphics/regions.h"
  56. #include "graphics/clip.h"
  57. #include "exec/exec.h"
  58. #include "graphics/text.h"
  59. #include "graphics/gfxbase.h"
  60.  
  61. #include "devices/console.h"
  62. #include "devices/keymap.h"
  63.  
  64. #include "libraries/dos.h"
  65. #include "graphics/text.h"
  66. #include "libraries/diskfont.h"
  67. #include "intuition/intuition.h"
  68.  
  69. UBYTE escdata[] = { 0x9b, '@',  /* insert character */
  70.             0x9b, 'A',    /* cursor up */
  71.             0x9b, 'B',  /* cursor down */
  72.             0x9b, 'C',  /* cursor left */
  73.             0x9b, 'D',  /* cursor right */
  74.             0x9b, 'E',  /* cursor next line */
  75.             0x9b, 'F',  /* cursor prev line */
  76.             0x9b, 'J',    /* erase to end of display */
  77.             0x9b, 'K',  /* erase to end of line */
  78.             0x9b, 'L',  /* insert line */
  79.             0x9b, 'M',  /* delete line */
  80.             0x9b, 'P',  /* delete character */
  81.             0x9b, 'S',  /* scroll up */
  82.             0x9b, 'T',  /* scroll down */
  83.             0x1b, 'c',  /* reset to initial state */
  84.             0x9b, 'q',  /* window status request */
  85.             0x9b, 'n',  /* device status report */
  86.             0x9b, ' ', 'p',/* cursor on */
  87.             0x9b, '0', ' ', 'p', /* cursor off */
  88.             0x9b, '2', '0', 'h', /* set mode */
  89.             0x9b, '2', '0', 'l', /* reset mode */
  90.            };
  91.  
  92.     /* COVER A SELECTED SUBSET OF THE CONSOLE AVAILABLE FUNCTIONS */
  93.  
  94. #define INSERTCHARSTRING     &escdata[0] 
  95. #define CURSUPSTRING        &escdata[0+2] 
  96. #define CURSDOWNSTRING         &escdata[0+4] 
  97. #define CURSFWDSTRING         &escdata[0+6] 
  98. #define CURSBAKSTRING         &escdata[0+8] 
  99. #define CURSNEXTLINE         &escdata[0+10] 
  100. #define CURSPREVLINE         &escdata[0+12] 
  101. #define ERASEEODSTRING         &escdata[0+14] 
  102. #define ERASEEOLSTRING         &escdata[0+16] 
  103. #define INSERTLINESTRING     &escdata[0+18] 
  104. #define DELETELINESTRING     &escdata[0+20] 
  105. #define DELCHARSTRING         &escdata[0+22] 
  106. #define SCROLLUPSTRING         &escdata[0+24] 
  107. #define SCROLLDOWNSTRING     &escdata[0+26] 
  108. #define RESETINITSTRING     &escdata[0+28]
  109. #define WINDOWSTATSTRING     &escdata[0+30]
  110. #define DEVSTATSTRING         &escdata[0+32]
  111. #define CURSONSTRING         &escdata[0+34] 
  112. #define CURSOFFSTRING         &escdata[0+37] 
  113. #define SETMODESTRING         &escdata[0+41]
  114. #define RESETMODESTRING     &escdata[0+45]
  115.  
  116. #define BACKSPACE(r)         ConPutChar(r,0x08)
  117. #define TAB(r)             ConPutChar(r,0x09)
  118. #define LINEFEED(r)         ConPutChar(r,0x0a)
  119. #define VERTICALTAB(r)         ConPutChar(r,0x0b)
  120. #define FORMFEED(r)         ConPutChar(r,0x0c)
  121. #define CR(r)             ConPutChar(r,0x0d)
  122. #define SHIFTOUT(r)         ConPutChar(r,0x0e)
  123. #define SHIFTIN(r)         ConPutChar(r,0x0f)
  124. #define CLEARSCREEN(r)        ConPutChar(r,0x0c)
  125.  
  126. #define RESET(r)         ConWrite(r,RESETINITSTRING,2)
  127. #define INSERT(r)         ConWrite(r,INSERTCHARSTRING,2)
  128. #define CURSUP(r)         ConWrite(r,CURSUPSTRING,2)
  129. #define CURSDOWN(r)         ConWrite(r,CURSDOWNSTRING,2)
  130. #define CURSFWD(r)         ConWrite(r,CURSFWDSTRING,2)
  131. #define CURSBAK(r)         ConWrite(r,CURSBAKSTRING,2)
  132. #define CURSNEXTLN(r)         ConWrite(r,CURSNEXTLINE,2)
  133. #define CURSPREVLN(r)         ConWrite(r,CURSPREVLINE,2)
  134. #define ERASEEOD(r)         ConWrite(r,ERASEEODSTRING,2)
  135. #define ERASEEOL(r)         ConWrite(r,ERASEEOLSTRING,2)
  136. #define INSERTLINE(r)         ConWrite(r,INSERTLINESTRING,2)
  137. #define DELETELINE(r)         ConWrite(r,DELETELINESTRING,2)
  138. #define SCROLLUP(r)         ConWrite(r,SCROLLUPSTRING,2)
  139. #define SCROLLDOWN(r)         ConWrite(r,SCROLLDOWNSTRING,2)
  140. #define DEVICESTATUS(r)     ConWrite(r,DEVSTATSTRING,2)
  141. #define WINDOWSTATUS(r)     ConWrite(r,WINDOWSTATSTRING,2)
  142. #define DELCHAR(r)         ConWrite(r,DELCHARSTRING,2)
  143. #define CURSORON(r)         ConWrite(r,CURSONSTRING,3)
  144. #define CURSOROFF(r)         ConWrite(r,CURSOFFSTRING,4)
  145. #define SETMODE(r)         ConWrite(r,SETMODESTRING,4)
  146. #define RESETMODE(r)         ConWrite(r,RESETMODESTRING,4)
  147.  
  148. #define CloseConsole(r) CloseDevice(r)
  149.  
  150. ULONG DosBase;
  151. ULONG DiskfontBase;
  152. ULONG IntuitionBase;
  153. ULONG GfxBase;
  154.  
  155.  
  156. struct NewWindow nw = {
  157.         10, 10,        /* starting position (left,top) */
  158.         620,90,        /* width, height */
  159.         -1,-1,          /* detailpen, blockpen */
  160.         0,              /* flags for idcmp */
  161.   WINDOWDEPTH|WINDOWSIZING|WINDOWDRAG|SIMPLE_REFRESH|ACTIVATE|GIMMEZEROZERO,
  162.                         /* window gadget flags */
  163.         0,              /* pointer to 1st user gadget */
  164.         NULL,           /* pointer to user check */
  165.         "Console Test", /* title */
  166.         NULL,           /* pointer to window screen */
  167.         NULL,           /* pointer to super bitmap */
  168.         100,45,         /* min width, height */
  169.         640,200,        /* max width, height */
  170.         WBENCHSCREEN};
  171.  
  172. struct Window *w;
  173. struct RastPort *rp;
  174.     
  175. struct IOStdReq *consoleWriteMsg;   /* I/O request block pointer */
  176. struct MsgPort *consoleWritePort;      /* a port at which to receive */    
  177. struct IOStdReq *consoleReadMsg;   /* I/O request block pointer */
  178. struct MsgPort *consoleReadPort;      /* a port at which to receive */    
  179.     
  180. extern struct MsgPort *CreatePort();
  181. extern struct IOStdReq *CreateStdIO();
  182.  
  183. char readstring[200];    /* provides a buffer even though using only one char */
  184.  
  185. main()
  186. {
  187.  
  188.     SHORT i;
  189.     SHORT status;
  190.     SHORT problem;
  191.     SHORT error;
  192.     problem = 0;
  193.  
  194.         if((DosBase = OpenLibrary("dos.library", 0)) == NULL) 
  195.         { problem = 1; goto cleanup1; }
  196.         if((DiskfontBase=OpenLibrary("diskfont.library",0))==NULL) 
  197.         { problem = 2; goto cleanup2; }
  198.         if((IntuitionBase=OpenLibrary("intuition.library",0))==NULL) 
  199.         { problem = 3; goto cleanup3; }
  200.         if((GfxBase=OpenLibrary("graphics.library",0))==NULL) 
  201.         { problem = 4; goto cleanup4; }
  202.  
  203.     consoleWritePort = CreatePort("my.con.write",0);
  204.     if(consoleWritePort == 0) 
  205.         { problem = 5; goto cleanup5; }
  206.     consoleWriteMsg =  CreateStdIO(consoleWritePort);
  207.     if(consoleWritePort == 0)
  208.         { problem = 6; goto cleanup6; }
  209.  
  210.     consoleReadPort = CreatePort("my.con.read",0);
  211.     if(consoleReadPort == 0) 
  212.         { problem = 7; goto cleanup7; }
  213.     consoleReadMsg =  CreateStdIO(consoleReadPort);
  214.     if(consoleReadPort == 0) 
  215.         { problem = 8; goto cleanup8; }
  216.  
  217.         w = (struct Window *)OpenWindow(&nw);    /* create a window */
  218.     if(w == NULL)
  219.         { problem = 9; goto cleanup9; }
  220.  
  221.         rp = w->RPort;        /* establish its rastport for later */
  222.  
  223. /* ************************************************************************ */
  224. /* NOW, Begin using the actual console macros defined above.                */    
  225. /* ************************************************************************ */
  226.  
  227.     error = OpenConsole(consoleWriteMsg,consoleReadMsg,w);    
  228.     if(error != 0)  
  229.         { problem = 10; goto cleanup10; }
  230.         /* attach a console to this window, initialize
  231.          * for both write and read */
  232.  
  233.     QueueRead(consoleReadMsg,&readstring[0]); /* tell console where to 
  234.                            * put a character that
  235.                            * it wants to give me
  236.                            * queue up first read */
  237.     ConWrite(consoleWriteMsg,"Hello, World\r\n",14);
  238.  
  239.     ConPutStr(consoleWriteMsg,"testing BACKSPACE");
  240.     for(i=0; i<10; i++)
  241.         { BACKSPACE(consoleWriteMsg); Delay(30); }
  242.  
  243.     ConPutStr(consoleWriteMsg,"\r\n");
  244.  
  245.     ConPutStr(consoleWriteMsg,"testing TAB\r");
  246.     for(i=0; i<6; i++)
  247.         { TAB(consoleWriteMsg); Delay(30); }
  248.  
  249.     ConPutStr(consoleWriteMsg,"\r\n");
  250.  
  251.     ConPutStr(consoleWriteMsg,"testing LINEFEED\r");
  252.     for(i=0; i<4; i++)
  253.         { LINEFEED(consoleWriteMsg); Delay(30); }
  254.  
  255.     ConPutStr(consoleWriteMsg,"\r\n");
  256.  
  257.     ConPutStr(consoleWriteMsg,"testing VERTICALTAB\r");
  258.     for(i=0; i<4; i++)
  259.         { VERTICALTAB(consoleWriteMsg); Delay(30); }
  260.  
  261.     ConPutStr(consoleWriteMsg,"\r\n");
  262.  
  263.     ConPutStr(consoleWriteMsg,"testing FORMFEED\r");
  264.     Delay(30);
  265.     for(i=0; i<2; i++)
  266.         { FORMFEED(consoleWriteMsg); Delay(30); }
  267.  
  268.     ConPutStr(consoleWriteMsg,"\r\n");
  269.  
  270.     ConPutStr(consoleWriteMsg,"testing CR");
  271.     Delay(30);
  272.     CR(consoleWriteMsg);
  273.     Delay(60);
  274.     ConPutStr(consoleWriteMsg,"\r\n");
  275.  
  276.     ConPutStr(consoleWriteMsg,"testing INSERT\r");
  277.     for(i=0; i<4; i++)
  278.         { INSERT(consoleWriteMsg); Delay(30); }
  279.  
  280.     ConPutStr(consoleWriteMsg,"\r\n");
  281.  
  282.     ConPutStr(consoleWriteMsg,"    testing DELCHAR\r");
  283.     CR(consoleWriteMsg);
  284.     for(i=0; i<4; i++)
  285.         { DELCHAR(consoleWriteMsg); Delay(30); }
  286.  
  287.     ConPutStr(consoleWriteMsg,"\r\n");
  288.  
  289.     ConPutStr(consoleWriteMsg,"testing INSERTLINE\r");
  290.     CR(consoleWriteMsg);
  291.     for(i=0; i<3; i++)
  292.         { INSERTLINE(consoleWriteMsg); Delay(30); }
  293.     ConPutStr(consoleWriteMsg,"\r\n");
  294.  
  295.     ConPutStr(consoleWriteMsg,"testing DELETELINE\r");
  296.     CR(consoleWriteMsg);
  297.     LINEFEED(consoleWriteMsg);
  298.     Delay(60);
  299.     for(i=0; i<4; i++)
  300.         { DELETELINE(consoleWriteMsg); Delay(30); }
  301.     ConPutStr(consoleWriteMsg,"\r\n");
  302.  
  303.     ConPutStr(consoleWriteMsg,"testing CURSUP\r");
  304.     for(i=0; i<4; i++)
  305.         { CURSUP(consoleWriteMsg); Delay(30); }
  306.  
  307.     ConPutStr(consoleWriteMsg,"\r\n");
  308.  
  309.     ConPutStr(consoleWriteMsg,"testing CURSDOWN\r");
  310.     for(i=0; i<4; i++)
  311.         { CURSDOWN(consoleWriteMsg); Delay(30); }
  312.  
  313.     ConPutStr(consoleWriteMsg,"\r\n");
  314.  
  315.     ConPutStr(consoleWriteMsg,"testing CURSFWD\r");
  316.     for(i=0; i<4; i++)
  317.         { CURSFWD(consoleWriteMsg); Delay(30); }
  318.  
  319.     ConPutStr(consoleWriteMsg,"\r\n");
  320.  
  321.     ConPutStr(consoleWriteMsg,"testing CURSBAK");
  322.     for(i=0; i<4; i++)
  323.         { CURSBAK(consoleWriteMsg); Delay(30); }
  324.  
  325.     ConPutStr(consoleWriteMsg,"\r\n");
  326.  
  327.     ConPutStr(consoleWriteMsg,"testing CURSPREVLN");
  328.     for(i=0; i<4; i++)
  329.         { CURSPREVLN(consoleWriteMsg); Delay(30); }
  330.  
  331.     ConPutStr(consoleWriteMsg,"\r\n");
  332.  
  333.     ConPutStr(consoleWriteMsg,"testing CURSNEXTLN");
  334.     for(i=0; i<4; i++)
  335.         { CURSNEXTLN(consoleWriteMsg); Delay(30); }
  336.  
  337.     ConPutStr(consoleWriteMsg,"\r\n");
  338.  
  339.     ConPutStr(consoleWriteMsg,"testing ERASEEOD");
  340.     CURSPREVLN(consoleWriteMsg);
  341.     CURSPREVLN(consoleWriteMsg);
  342.     CURSPREVLN(consoleWriteMsg);
  343.     Delay(60);
  344.     for(i=0; i<4; i++)
  345.         { ERASEEOD(consoleWriteMsg); Delay(30); }
  346.  
  347.     ConPutStr(consoleWriteMsg,"\r\n");
  348.  
  349.     ConPutStr(consoleWriteMsg,"testing ERASEEOL.junk");
  350.     CURSBAK(consoleWriteMsg);
  351.     CURSBAK(consoleWriteMsg);
  352.     CURSBAK(consoleWriteMsg);
  353.     CURSBAK(consoleWriteMsg);
  354.     CURSBAK(consoleWriteMsg);
  355.     Delay(60);
  356.     ERASEEOL(consoleWriteMsg); 
  357.     Delay(30);
  358.     ConPutStr(consoleWriteMsg,"\r\n");
  359.  
  360.     ConPutStr(consoleWriteMsg,"testing SCROLLUP");
  361.     for(i=0; i<4; i++)
  362.         { SCROLLUP(consoleWriteMsg); Delay(30); }
  363.  
  364.     ConPutStr(consoleWriteMsg,"\r\n");
  365.     ConPutStr(consoleWriteMsg,"testing SCROLLDOWN");
  366.     ConPutStr(consoleWriteMsg,"\n\n\n");
  367.     for(i=0; i<4; i++)
  368.         { SCROLLDOWN(consoleWriteMsg); Delay(30); }
  369.  
  370.     ConPutStr(consoleWriteMsg,"\r\n");
  371.  
  372.     ConPutStr(consoleWriteMsg,"testing CURSOROFF");
  373.     CURSOROFF(consoleWriteMsg); 
  374.     ConPutStr(consoleWriteMsg, "printed.with.cursor.off"); 
  375.     Delay(60);
  376.     ConPutStr(consoleWriteMsg,"\r\n");
  377.  
  378.     CURSORON(consoleWriteMsg); Delay(30);
  379.     ConPutStr(consoleWriteMsg,"testing CURSORON");
  380.  
  381.  
  382. /* ************************************************************************ */
  383.     Delay(120);    /* wait 2 seconds (120/60 ticks) */
  384.  
  385.     status = CheckIO(consoleReadMsg);    /* see if console read
  386.                          * anything, abort if not */
  387.     if(status == FALSE) AbortIO(consoleReadMsg);
  388.     WaitPort(consoleReadPort);    /* wait for abort to complete */
  389.     GetMsg(consoleReadPort);    /* and strip message from port */
  390.  
  391.     CloseConsole(consoleWriteMsg);
  392.    cleanup10:
  393.    cleanup9:
  394.     CloseWindow(w);
  395.    cleanup8:
  396.     DeleteStdIO(consoleReadMsg);
  397.    cleanup7:
  398.     DeletePort(consoleReadPort);
  399.    cleanup6:
  400.     DeleteStdIO(consoleWriteMsg);
  401.    cleanup5:
  402.     DeletePort(consoleWritePort);
  403.    cleanup4:
  404.         CloseLibrary(GfxBase);
  405.    cleanup3:
  406.     CloseLibrary(IntuitionBase);
  407.    cleanup2:
  408.         CloseLibrary(DiskfontBase);
  409.    cleanup1:
  410.         CloseLibrary(DosBase);
  411.     if(problem > 0) exit(problem+1000);
  412.     else
  413.         return(0);
  414.  
  415. }    /* end of main() */
  416.  
  417.  
  418. /* Open a console device */
  419.  
  420.     /* this function returns a value of 0 if the console 
  421.      * device opened correctly and a nonzero value (the error
  422.      * returned from OpenDevice) if there was an error.
  423.      */
  424.     
  425.     int
  426. OpenConsole(writerequest,readrequest,window)
  427.     struct IOStdReq *writerequest;
  428.     struct IOStdReq *readrequest;
  429.     struct Window *window;
  430.     {    
  431.         int error;    
  432.             writerequest->io_Data = (APTR) window;
  433.             writerequest->io_Length = sizeof(*window);
  434.             error = OpenDevice("console.device", 0, writerequest, 0);
  435.         readrequest->io_Device = writerequest->io_Device;
  436.         readrequest->io_Unit   = writerequest->io_Unit;
  437.             /* clone required parts of the request */
  438.         return(error);    
  439.     }
  440.  
  441. /* Output a single character to a specified console */ 
  442.  
  443.     int
  444. ConPutChar(request,character)
  445.     struct IOStdReq *request;
  446.     char character;
  447.     {
  448.             request->io_Command = CMD_WRITE;
  449.             request->io_Data = (APTR)&character;
  450.             request->io_Length = 1;
  451.             DoIO(request);
  452.         /* command works because DoIO blocks until command is
  453.          * done (otherwise pointer to the character could become
  454.          * invalid in the meantime).
  455.          */
  456.         return(0);
  457.     }
  458.  
  459. /* Output a stream of known length to a console */ 
  460.  
  461.     int
  462. ConWrite(request,string,length)
  463.     struct IOStdReq *request;
  464.     char *string;
  465.     int length;
  466.     {
  467.             request->io_Command = CMD_WRITE;
  468.             request->io_Data = (APTR)string;
  469.             request->io_Length = length; 
  470.             DoIO(request);
  471.         /* command works because DoIO blocks until command is
  472.          * done (otherwise pointer to string could become
  473.          * invalid in the meantime).
  474.          */
  475.         return(0);
  476.     }
  477.  
  478. /* Output a NULL-terminated string of characters to a console */ 
  479.  
  480.     int
  481. ConPutStr(request,string)
  482.     struct IOStdReq *request;
  483.     char *string;
  484.     {
  485.             request->io_Command = CMD_WRITE;
  486.             request->io_Data = (APTR)string;
  487.             request->io_Length = -1;  /* tells console to end when it
  488.                        * sees a terminating zero on
  489.                        * the string. */
  490.             DoIO(request);
  491.         return(0);
  492.     }
  493.     
  494.     /* queue up a read request to a console, show where to
  495.      * put the character when ready to be returned.  Most
  496.      * efficient if this is called right after console is
  497.      * opened */
  498.  
  499.     int
  500. QueueRead(request,whereto)
  501.     struct IOStdReq *request;
  502.     char *whereto;
  503.     {
  504.             request->io_Command = CMD_READ;
  505.             request->io_Data = (APTR)whereto;
  506.             request->io_Length = 1;
  507.             SendIO(request);
  508.         return(0);
  509.     }
  510.  
  511.     /* see if there is a character to read.  If none, don't wait, 
  512.      * come back with a value of -1 */
  513.  
  514.     int 
  515. ConMayGetChar(request,requestPort, whereto)
  516.     struct IOStdReq *request;
  517.     char *whereto;
  518.     {
  519.             register temp;
  520.  
  521.             if ( GetMsg(requestPort) == NULL ) return(-1);
  522.             temp = *whereto;
  523.             QueueRead(request,whereto);
  524.             return(temp);
  525.     }
  526.  
  527.     /* go and get a character; put the task to sleep if
  528.      * there isn't one present */
  529.  
  530.     UBYTE
  531. ConGetChar(consolePort,request,whereto)
  532.     struct IOStdReq *request; 
  533.     struct MsgPort *consolePort;   
  534.         char *whereto;
  535.     {
  536.         register UBYTE temp;
  537.         while((GetMsg(consolePort) == NULL)) WaitPort(consolePort);
  538.         temp = *whereto;    /* get the character */
  539.         QueueRead(request,whereto);
  540.         return(temp);
  541.     }
  542.             
  543.  
  544.